package com.swh;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.util.Date;

/**
 * @Author: swh
 * @Description:
 * @Date: 2021/1/18 19:08
 * @Version: 1.0
 */
public class Demo1_CreateJWT {

    @Test
    public void test() {
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("11")
                .setIssuer("me")                              //签发者
                .setAudience("you")                           //接收方
                .setSubject("小准")                          //用户名
                .setIssuedAt(new Date())                     //签发时间
                .signWith(SignatureAlgorithm.HS256, "dfbz")//设置秘钥,并指定头部算法
                .setExpiration(new Date(new Date().getTime() + 30 * 1000)) //过期时间(30秒后过期)
                .claim("role", "admin")//自定义用户信息
                .claim("mobile", "110");//自定义用户信息

        System.out.println(jwtBuilder.compact());


    }

    @Test
    public void test02() {

        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMSIsImlzcyI6Im1lIiwiYXVkIjoieW91Iiwic3ViIjoi5bCP5YeGIiwiaWF0IjoxNjEwOTY4OTU1LCJleHAiOjE2MTA5Njg5ODUsInJpbGUiOiJhZG1pbiIsIm1vYmlsZSI6IjExMCJ9.MtA2Nw2EOYeOE_rYOJ5CGMctZEX0hoO6HRuhUcbHoKA";

        Claims claims = Jwts.parser().
                setSigningKey("dfbz").      //当初签发时的秘钥
                parseClaimsJws(token).      //token字符串
                getBody();

        System.out.println("id: "+claims.getId());
        System.out.println("Issuer: "+claims.getIssuer());          //签发者
        System.out.println("Audience: "+claims.getAudience());      //接收方
        System.out.println("Subject: "+claims.getSubject());        //用户名
        System.out.println("IssuedAt: "+claims.getIssuedAt());      //签发时间
        System.out.println("Expiration: "+claims.getExpiration());  //查看过期时间
        System.out.println("role: "+claims.get("role"));            //查看自定义信息role
        System.out.println("mobile: "+claims.get("mobile"));        //查看自定义信息mobile

    }

}
